package web.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import web.bean.Tousu;
import web.bean.XiaoQu;
import web.bean.Zichan;
import web.dao.TouSuDao;
import web.service.Impl.TouSuServiceImpl;
import web.service.Impl.XiaoQuServiceImpl;
import web.service.TouSuService;
import web.service.XiaoQuService;
import web.utils.R;
import web.vo.PageVo;

public class TouSuController {
TouSuService touSuService=new TouSuServiceImpl();
XiaoQuService xiaoQuService=new XiaoQuServiceImpl();
    public R page(JSONObject jsonObject){
        Integer pageSize=jsonObject.getInteger("pageSize");
        Integer pageNo=jsonObject.getInteger("pageNo");
        String startTime=jsonObject.getString("startTime");
        String endTime=jsonObject.getString("endTime");
        String name=jsonObject.getString("name");
        PageVo<Tousu> byInfo = touSuService.findByInfo(pageSize, pageNo, startTime, endTime, name);
        return R.ok().setData(byInfo);
    }
    public R add(JSONObject jsonObject){
        String s = jsonObject.toJSONString();
        Tousu tousu = JSONObject.parseObject(s, Tousu.class);
        Integer id=tousu.getXiaoquId();
        XiaoQu suoshuxiaoqu = xiaoQuService.findNameById(id);
        tousu.setSuoshuxiaoqu(suoshuxiaoqu.getXiaoqumingcheng());
        int rs = touSuService.add(tousu);
        if (rs>0){
            return R.ok("插入成功！");
        }else {
            return R.error("插入失败");
        }
    }
    public R del(JSONObject jsonObject){
        JSONArray ids=jsonObject.getJSONArray("id");
        Integer[] delIds=new  Integer[ids.size()];
        for (int i = 0; i < ids.size(); i++) {
            delIds[i]=ids.getInteger(i);
        }
        int rs=touSuService.del(delIds);
        if (rs>0){
            return R.ok("删除成功！");
        }else {
            return R.error("删除失败！");
        }
    }
    public R edit(JSONObject jsonObject){
        String s = jsonObject.toJSONString();
        Tousu tousu = JSONObject.parseObject(s, Tousu.class);
        Integer xiaoquId=tousu.getXiaoquId();
        XiaoQu suoshuxiaoqu = xiaoQuService.findNameById(xiaoquId);
        tousu.setSuoshuxiaoqu(suoshuxiaoqu.getXiaoqumingcheng());
        int i = touSuService.editInfo(tousu);
        if(i>0){
            return R.ok("更新成功");
        }else{
            return R.error("更新失败");
        }
    }
    public R findTouSu(JSONObject jsonObject){
        Integer id=jsonObject.getInteger("id");
        Tousu touSu = touSuService.findTouSu(id);
        return R.ok().setData(touSu);
    }

}
